package app.android.kdictionary.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import app.android.kdictionary.R;
import app.android.kdictionary.Util.CheckNetworkConnection;
import app.android.kdictionary.enums.NetworkConnectionType;

public class TitleActivity extends Activity {
//	private Handler handler;
	private Thread thread;
	private final int CHARGE_DIALOG = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);

        thread = new Thread(new Sleep(this, mainhandler));
        thread.start();
//        ((ImageView)findViewById(R.id.titleImage)).setOnClickListener(clickTitle);
    }
	
	@Override
	protected android.app.Dialog onCreateDialog(int id) {
		switch(id) {
			case CHARGE_DIALOG :
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle(R.string.charge_dialog_title);
				builder.setMessage(R.string.charge_dialog_message);
				builder.setPositiveButton(R.string.charge_dialog_positive, chargeDialogPositiveOnClickListener);
				builder.setNegativeButton(R.string.charge_dialog_negative, chargeDialogNegativeOnClickListener);
				builder.setCancelable(false);
				
				AlertDialog alert = builder.create();
				return alert;
		}
		return null;
	};
	
	DialogInterface.OnClickListener chargeDialogPositiveOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			startActivity(new Intent(TitleActivity.this, HeadwordActivity.class));
			finish();
		}
	};
	
	DialogInterface.OnClickListener chargeDialogNegativeOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			finish();
		}
	};
	
//	private OnClickListener clickTitle = new OnClickListener() {
//
//		@Override
//		public void onClick(View v) {
//			if(thread.isAlive()) {
//				thread.interrupt();
//			}
//			startActivity(new Intent(TitleActivity.this, HeadwordActivity.class));
//			finish();
//		}
//	};
	
	private Handler mainhandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			NetworkConnectionType connectionType = (NetworkConnectionType)msg.obj;
			switch(connectionType) {
				case NETWORK_AVAIL :
					startActivity(new Intent(TitleActivity.this, HeadwordActivity.class));
					finish();
					break;
				case NETWORK_CONNECTION :
					Toast.makeText(TitleActivity.this, "인터넷이 연결되어 있지 않습니다.", Toast.LENGTH_SHORT).show();
					break;
				case NETWORK_ERROR :
					Toast.makeText(TitleActivity.this, "인터넷을 이용할 수 없습니다.", Toast.LENGTH_SHORT).show();
					break;
				case NETWORK_CHARGE :
					showDialog(CHARGE_DIALOG);
					break;
			}
		}
	};
	
}
class Sleep implements Runnable {

	private Handler handler;
	private Activity activity;
	
	public Sleep(Activity activity, Handler handler) {
		this.activity = activity;
		this.handler = handler;
	}
	
	@Override
	public void run() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		Message msg = new Message();
		NetworkConnectionType checkResult = CheckNetworkConnection.checkNetworkConnection(activity);
		msg.obj = checkResult;
		handler.sendMessage(msg);
	}
}
